home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / pmpsrc11.zip / HEARD.C < prev    next >
Text File  |  1991-07-30  |  4KB  |  167 lines

  1. /*
  2.     heard.c -- Stations/nodes heard logging routines
  3.  
  4.   Poor Man's Packet (PMP)
  5.   Copyright (c) 1991 by Andrew C. Payne    All Rights Reserved.
  6.  
  7.   Permission to use, copy, modify, and distribute this software and its
  8.   documentation without fee for NON-COMMERCIAL AMATEUR RADIO USE ONLY is hereby
  9.   granted, provided that the above copyright notice appear in all copies.
  10.   The author makes no representations about the suitability of this software
  11.   for any purpose.  It is provided "as is" without express or implied warranty.
  12.  
  13.     October, 1989
  14.     Andrew C. Payne
  15. */
  16. /* ----- Includes ----- */
  17. #include <stdio.h>
  18. #include <alloc.h>
  19. #include <time.h>
  20. #include <mem.h>
  21. #include <string.h>
  22. #include "pmp.h"
  23. #include "keys.h"
  24. #include "heard.h"
  25.  
  26. static struct mheard *heard;    /* MHEARD list */
  27. int    nheard;                 /* number in mheard list */
  28.  
  29. extern KEY WaitKey(int prompt);
  30.  
  31. /* HeardInit()
  32.     Initialize the HEARD list structure.
  33. */
  34. void HeardInit()
  35. {
  36.     heard = malloc(MAXHEARD * sizeof(struct mheard));
  37.     if(heard == NULL)
  38.         OutOfMemory();
  39.     nheard = 0;
  40. }
  41.  
  42. /* Heard(l2)
  43.     Heard list upcall for level 2 packets.  This routine is called once
  44.     for all received level 2 packets.
  45. */
  46. void Heard(struct ax25_packet *p)
  47. {
  48.     int    i;
  49.     time_t    oldest;
  50.     int    noldest;
  51.     int    type;
  52.  
  53.     if(nheard) {
  54.         oldest = 0x7fffffffL;
  55.         for(i=0; i<nheard; i++) {
  56.             if(!CompAX25Addr(&p->source,&heard[i].call))
  57.                 goto update;
  58.             if(heard[i].last < oldest) {
  59.                 oldest = heard[i].last;
  60.                 noldest = i;
  61.             }
  62.         }
  63.         if(nheard < MAXHEARD)
  64.             i = nheard++;
  65.         else
  66.             i = noldest;    /* use oldest entry */
  67.     } else
  68.         i = nheard++;
  69.  
  70.     memcpy(&heard[i].call, &p->source, sizeof(struct ax25_addr));
  71.     heard[i].bytes = heard[i].flags = heard[i].count = 0;
  72.     heard[i].id[0] = '\0';
  73.  
  74. update:
  75.     heard[i].last = time(NULL);
  76.     heard[i].count++;
  77.  
  78.     type = FrameType(p->cont);
  79.     switch(type) {
  80.         case I:
  81.         case UI:
  82.             heard[i].bytes += p->dlen;
  83.             heard[i].flags |= HEARD_INFO;
  84.             switch(p->pid) {
  85.                 case PID_NETROM:
  86.                     heard[i].flags |= HEARD_NETROM;
  87.                     break;
  88.             }
  89.     }
  90.     if(p->ndigis == 0)
  91.         heard[i].flags |= HEARD_DIRECT;
  92. }
  93.  
  94. /* timestr(t)
  95.     Given a time in seconds since Jan 1, 1970.
  96. */
  97. static char *timestr(time_t t)
  98. {
  99.     static char     s[30];
  100.     struct tm    *timeptr;
  101.  
  102.     timeptr = localtime(&t);
  103.     sprintf(s,"%02d:%02d:%02d",
  104.         timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
  105.  
  106.     return s;
  107. }
  108.  
  109. /* DoHeard()
  110.     Perform the nodes heard command
  111.  
  112.     (Probably should be in PMP.C)
  113. */
  114. void DoHeard(void)
  115. {
  116.     int    i,j,t;
  117.     char    s[80];
  118.     int    sort[MAXHEARD];
  119.     struct mheard    *p;
  120.  
  121.     SaveScreen(TRUE,TRUE);
  122.     CenterTitle(1,"   Nodes Recently Heard   ");
  123.  
  124.     GotoXY(1,2);
  125.     if(nheard) {
  126.         _uputs(NormalAttr,"│ Node          Time    Count  Bytes  Flags\n\n");
  127.  
  128. /* sort mheard list by time heard */
  129.         for(i=0; i<nheard; i++)
  130.             sort[i] = i;
  131.         if(nheard > 1) {
  132.             for(i=nheard-1; i>0; i--) {
  133.                 for(j=0; j<i; j++) {
  134.                     if(heard[sort[j]].last < heard[sort[j+1]].last) {
  135.                         t = sort[j];
  136.                         sort[j] = sort[j+1];
  137.                         sort[j+1] = t;
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.  
  143. /* show mheard list */
  144.         for(i=0; i<nheard; i++) {
  145.             p = heard + sort[i];
  146.             sprintf(s,"│ %-9s%c  %s  %4ld  %6ld  ",
  147.                 GetAX25Addr(&p->call),
  148.                 (p->flags & HEARD_DIRECT) ? ' ' : '*',
  149.                 timestr(p->last),
  150.                 p->count,
  151.                 p->bytes);
  152.             j = p->flags;
  153.             if(j & HEARD_INFO)
  154.                 strcat(s,"Text ");
  155.             if(j & HEARD_IP)
  156.                 strcat(s,"IP ");
  157.             if(j & HEARD_NETROM)
  158.                 strcat(s,"NET/ROM ");
  159.             strcat(s,"\n");
  160.             _uputs(NormalAttr,s);
  161.         }
  162.     } else
  163.         _uputs(NormalAttr,"│      ***** None heard *****");
  164.  
  165.     (void)WaitKey(23);
  166.     RestoreScreen();
  167. }